/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GeneralManager.Logic;

import Data.DAO.IDataStore;
import data.DAO.IEquivalentCouponStore;
import data.pojo.Book;
import data.pojo.EquivalentCoupon;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author Kite
 */
public class EquivalentManage {
    
    private List<EquivalentCoupon> newList = new ArrayList<EquivalentCoupon>();
    private List<EquivalentCoupon> editList = new ArrayList<EquivalentCoupon>();
    private List<EquivalentCoupon> removeList = new ArrayList<EquivalentCoupon>();
    private List<EquivalentCoupon> searchList = new ArrayList<EquivalentCoupon>();
    private IDataStore equivalentDS = new IEquivalentCouponStore();   
    private ArrayList<Integer> indexes = new ArrayList<Integer>();
    private ArrayList<EquivalentCoupon> allEquivalens;

    public EquivalentManage() {
        allEquivalens = (ArrayList<EquivalentCoupon>) equivalentDS.getObjects();
        newList = allEquivalens;
    }


    public Vector<List> searchTableVector(String key, String keywords) {
        searchList = equivalentDS.searchObjectsByAttr(key, ".*" + keywords + ".*");
        return new Vector<List>(toEquivalentTable(searchList));
    }
    //add


    public boolean addToNewList(EquivalentCoupon book) {
        if (this.isValid(book)) {
            this.newList.add(book);
            return true;
        } else {
            return false;
        }
    }

    private boolean isValid(EquivalentCoupon equivalent) {
        if (this.equivalentDS.searchObjectsByAttr("id", String.valueOf(equivalent.getId())).size()>1) {
            return false;
        }
        for (EquivalentCoupon e : newList) {
            if (e.getId() == equivalent.getId()) {
                return false;
            }
        }
        return true;
    }


    public boolean commitNewList() {
        if (this.newList.isEmpty()) {
            return false;
        } else {
            this.equivalentDS.update(newList);
            newList = (ArrayList<EquivalentCoupon>) equivalentDS.getObjects();
            return true;
        }
    }
    
    //edit

    public EquivalentCoupon getBySearch(int index) {
        return this.searchList.get(index);
    }
 

    public int getIndexInAll(EquivalentCoupon ec) {
        for(int i=0; i<allEquivalens.size(); i++){
            //System.out.println(allEquivalens.get(i).getId()+"    "+ec.getId());
            if(allEquivalens.get(i).getId().equals(ec.getId())){
                return i;
            }
        }
        return -1;
    }
    
   public boolean addToEditList(int index,EquivalentCoupon ec) {
        if (this.isValid(ec)) {
            this.indexes.add(index);
            this.editList.add(ec);
            return true;
        } else {
            return false;
        }
    }
   
    public boolean commitEditList() {
        for(int i=0; i<indexes.size(); i++){
            allEquivalens.set(this.indexes.get(i), this.editList.get(i));
        }
        equivalentDS.update(allEquivalens);
        this.editList.clear();
        this.indexes.clear();
        return true;
    }
    //remove
       public boolean addToRemoveList(int index,EquivalentCoupon ec) {
        if (this.isValid(index)) {
            this.indexes.add(index);
            this.removeList.add(ec);
            return true;
        } else {
            return false;
        }
    }
       
       
       
    private boolean isValid(int index) {
        for (Integer in : indexes) {
            if (in==index) {
                return false;
            }
        }
        return true;
    }
        public boolean commitRemList() {
         for(EquivalentCoupon ec: removeList){
            for(int i=0; i<allEquivalens.size(); i++){
                if(ec.getId().equals(allEquivalens.get(i).getId())){
                    allEquivalens.remove(i);
                }
            }
        }
        equivalentDS.update(allEquivalens);
        removeList = new ArrayList<EquivalentCoupon>();
        return true;
    }
    
    
    //table
    public Vector<List> equivalentTableVector() {
        return new Vector<List>(toEquivalentTable(newList));
    }

    public Vector<Vector<String>> toEquivalentTable(List list) {
        try {
            Vector<EquivalentCoupon> seList = new Vector(list);
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for (int i = 0; i < seList.size(); i++) {
                reList.add((Vector<String>) toEquivalentStringArray(seList.get(i)));
            }
            return reList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List toEquivalentStringArray(EquivalentCoupon equivalent) {
        Vector<String> list = new Vector<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        list.add(equivalent.getId());//Id
        
        list.add(((Double)equivalent.getPrice()).toString());//Author
        list.add(((Integer)equivalent.getMaxnumber()).toString());//title
        list.add(sdf.format(equivalent.getStartDay()));//publicationDate
        list.add(sdf.format(equivalent.getEndDay()));//publicationDate
        
        list.add(((Integer)equivalent.getNumber()).toString());
        return list;
    }

 
    
}
